<template>
  <div id="app">
    <div class="container">
      <div class="close"><span class="iconfont iconicon-test"></span></div>
      <div class="logo"><span class="iconfont iconnew"></span></div>
      <div class="inputs">
        <!-- <input placeholder="请输入手机号" class="input" /> -->
        <hmInput
          type="text"
          v-model="user.username"
          placeholder="请输入手机号"
          :reg="/^1[3,4,5,6,7,8,9]\d{9}$/"
          ref="username"
        ></hmInput>
        <hmInput
          type="password"
          v-model="user.password"
          placeholder="请输入密码"
          :reg="/^\d{3,}$/"
          ref="password"
        ></hmInput>
        <!-- <input
          placeholder="密码"
          class="input"
          type="password"
        /> -->
      </div>
      <p class="tips">
        没有账号？
        <a href="#/register" class="">去注册</a>
      </p>
      <!-- <div class="button">登录按钮</div> -->
      <van-button
        icon="star-o"
        color="red"
        round
        type="primary"
        block
        @click="loginFn"
        >登录</van-button
      >
    </div>
  </div>
</template>

<script>
// 导入封装好的登录请求
import { login } from "../api/user";
export default {
  data() {
    return {
      user: {
        username: "15270282489",
        password: "123",
      },
    };
  },
  methods: {
    // 登录
    loginFn() {
      console.log(1234);
      // 请求数据前，先判断，用户是否书写格式正确
      console.log(this.$refs.username.isOk);
      if (!this.$refs.username.isOk) {
        this.$toast.fail({
          message: "请重新输入正确的手机号码",
          icon: "fire-o",
        });
        return;
      }
      if (!this.$refs.password.isOk) {
        this.$toast.fail({
          message: "请输入3-9位数的密码",
          icon: "fire-o",
        });
        return;
      }

      login(this.user)
        .then((res) => {
          console.log(60, res);
          if (res.data.message == "登录成功") {
            // this.$toast.success(res.data.message);
            this.$toast.success({
              message: res.data.message,
              icon: "like-o",
            });
            // 登录成功后，保存token，和用户id
            localStorage.setItem("my-hm-token", res.data.data.token);
            localStorage.setItem("my-hm-id", res.data.data.user.id);
            this.$router.push("/index");
          } else {
            // this.$toast.fail(res.data.message);
            this.$toast.fail({
              message: res.data.message,
              icon: "fire-o",
              color: "red",
            });
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 20px;
}
.close {
  span {
    font-size: (27 / 360) * 100vw;
  }
}
.logo {
  display: flex;
  justify-content: center;

  span {
    display: block;
    font-size: (126 / 360) * 100vw;
    color: #d81e06;
  }
}
.inputs {
  input {
    margin-bottom: 20px;
  }
}
.tips {
  text-align: right;
  margin-bottom: 20px;
  a {
    color: #3385ff;
  }
}
</style>